home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EditorSaveAsCharset.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  4.9 KB  |  176 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  * Frank Tang  ftang@netscape.com
  22.  * J.M  Betak  jbetak@netscape.com
  23.  */
  24.  
  25.  
  26. var charset="";
  27. var titleWasEdited = false;
  28. var charsetWasChanged = false;
  29. var insertNewContentType = false;
  30. var contenttypeElement;
  31. var initDone = false;
  32.  
  33. //Cancel() is in EdDialogCommon.js
  34.  
  35. function Startup()
  36. {
  37.   if (!InitEditorShell())
  38.     return;
  39.  
  40.   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  41.   observerService.notifyObservers(null, "charsetmenu-selected", "other");
  42.  
  43.   gDialog.TitleInput    = document.getElementById("TitleInput");
  44.   gDialog.charsetTree   = document.getElementById('CharsetTree'); 
  45.   gDialog.exportToText  = document.getElementById('ExportToText');
  46.  
  47.   contenttypeElement = GetHTTPEquivMetaElement("content-type");
  48.   if(!contenttypeElement && (editorShell.contentsMIMEType != 'text/plain')) 
  49.   {
  50.     contenttypeElement = CreateHTTPEquivMetaElement("content-type");
  51.     if( ! contenttypeElement ) 
  52.     {
  53.       window.close();
  54.       return;
  55.     }
  56.     insertNewContentType = true;
  57.   }
  58.  
  59.   InitDialog();
  60.  
  61.   // Use the same text as the messagebox for getting title by regular "Save"
  62.   document.getElementById("EnterTitleLabel").setAttribute("value",GetString("NeedDocTitle"));
  63.   // This is an <HTML> element so it wraps -- append a child textnode
  64.   var helpTextParent = document.getElementById("TitleHelp");
  65.   var helpText = document.createTextNode(GetString("DocTitleHelp"));
  66.   if (helpTextParent)
  67.     helpTextParent.appendChild(helpText);
  68.   
  69.   // SET FOCUS TO FIRST CONTROL
  70.   SetTextboxFocus(gDialog.TitleInput);
  71.   
  72.   LoadAvailableCharSets();
  73.   initDone = true;
  74.   
  75.   SetWindowLocation();
  76. }
  77.  
  78.   
  79. function InitDialog() 
  80. {
  81.   gDialog.TitleInput.value = editorShell.GetDocumentTitle();
  82.   charset = editorShell.GetDocumentCharacterSet();
  83. }
  84.  
  85.  
  86. function onAccept()
  87. {
  88.   editorShell.BeginBatchChanges();
  89.  
  90.   if(charsetWasChanged) 
  91.   {
  92.      SetMetaElementContent(contenttypeElement, "text/html; charset=" + charset, insertNewContentType);     
  93.      editorShell.SetDocumentCharacterSet(charset);
  94.   }
  95.  
  96.   editorShell.EndBatchChanges();
  97.  
  98.   if(titleWasEdited) 
  99.     window.opener.newTitle = TrimString(gDialog.TitleInput.value);
  100.  
  101.   window.opener.ok = true;
  102.   window.opener.exportToText = gDialog.exportToText.checked;
  103.   SaveWindowLocation();
  104.   return true;
  105. }
  106.  
  107.  
  108. function readRDFString(aDS,aRes,aProp) 
  109. {
  110.   var n = aDS.GetTarget(aRes, aProp, true);
  111.   if (n)
  112.     return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  113.   else
  114.     return "";
  115. }
  116.  
  117.       
  118. function LoadAvailableCharSets()
  119. {
  120.   try 
  121.   {                                  
  122.     var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  123.     var kNC_Root = rdf.GetResource("NC:DecodersRoot");
  124.     var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  125.     var rdfDataSource = rdf.GetDataSource("rdf:charset-menu");
  126.     var rdfContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
  127.  
  128.     rdfContainer.Init(rdfDataSource, kNC_Root);
  129.  
  130.     var availableCharsets = rdfContainer.GetElements();
  131.     var charsetNode;
  132.     var selectedItem;
  133.     var item;
  134.  
  135.     ClearTreelist(gDialog.charsetTree);
  136.  
  137.     for (var i = 0; i < rdfContainer.GetCount(); i++) 
  138.     {
  139.       charsetNode = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  140.       item = AppendStringToTreelist(gDialog.charsetTree, readRDFString(rdfDataSource, charsetNode, kNC_name));
  141.       item.firstChild.firstChild.setAttribute("value", charsetNode.Value);
  142.       if(charset == charsetNode.Value) 
  143.         selectedItem = item;
  144.     }
  145.  
  146.     if(selectedItem) 
  147.     {
  148.         var index = gDialog.charsetTree.contentView.getIndexOfItem(selectedItem);
  149.       gDialog.charsetTree.treeBoxObject.selection.select(index);
  150.       gDialog.charsetTree.treeBoxObject.ensureRowIsVisible(index);
  151.     }
  152.   }
  153.   catch(e) { }
  154. }
  155.  
  156.  
  157. function SelectCharset()
  158. {
  159.   if(initDone) 
  160.   {
  161.     try 
  162.     {
  163.       charset = GetSelectedTreelistAttribute(gDialog.charsetTree, "value");
  164.       if(charset != "")
  165.          charsetWasChanged = true;
  166.     }
  167.     catch(e) {}
  168.   }
  169. }
  170.  
  171.  
  172. function TitleChanged()
  173. {
  174.   titleWasEdited = true; 
  175. }
  176.